package com.yanqu.road.server.manger.maidian.grandchild;

import com.yanqu.data.analytics.constant.EventDataConstants;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.grandchild.GrandChildData;
import com.yanqu.road.entity.grandchild.GrandChildOfficial;
import com.yanqu.road.entity.grandchild.GrandChildProfession;
import com.yanqu.road.entity.grandchild.SchoolSiteData;
import com.yanqu.road.entity.task.TaskInfo;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.grandson.GrandChildModule;
import com.yanqu.road.server.logic.task.BaseTaskData;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.config.grandchild.GrandChildConfigMgr;
import com.yanqu.road.server.manger.maidian.DefaultEventTraceAction;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 山庄埋点
 */
public class GrandChildLog {

    private static Logger logger = LogManager.getLogger(GrandChildLog.class.getName());

    //孙子获取
    public static final String GET_GRANDSON = "get_grandson";
    //书院扩建
    public static final String ACADEMY_EXTEND = "academy_extend";
    //书院培养
    public static final String ACADEMY_SCHOOL = "academy_school";
    //科举结束
    public static final String EXAMINATION = "examination";
    //族谱成就
    public static final String GENEALOGY_ACHIEVEMENT = "Genealogy_achievement";

    public static final String GRANDSON_NUMBER = "grandson_number";
    public static final String GRANDSON_QUALITY = "grandson_quality";
    public static final String GRANDSON_INTELLIGENCE = "grandson_intelligence";
    public static final String GRANDSON_TALENT = "grandson_talent";
    public static final String PICTURE_ID = "picture_id";
    public static final String BEFORE_SEAT = "before_seat";
    public static final String AFTER_SEAT = "after_seat";
    public static final String SCHOOL_ID = "school_id";
    public static final String START_TIME = "start_time";
    public static final String END_TIME = "end_time";
    public static final String IS_END = "is_end";

    public static final String VOCATION = "grandson_vocation";
    public static final String START_SPEED = "start_speed";
    public static final String END_SPEED = "end_speed";
    public static final String SERVER_PARTICIPAT = "server_Participat";
    public static final String EXAMINATION_NUMBER = "examination_number";
    public static final String GRANDSON_SPEED = "grandson_speed";
    public static final String EXAMINATION_RESULT = "examination_result";
    public static final String GRANDSON_ACHIEVEMENT_TYPE = "grandson_achievement_type";
    public static final String ACHIEVEMENT_ID = "achievement_id";
    public static final String GRANDSON_QUANTITY = "grandson_quantity";
    public static final String RECEIVE_ID = "receive_id";


    /**
     * 孙子获取
     */
    public static void getGrandson(GamePlayer player, GrandChildData childData){
        try {
            DefaultEventTraceAction traceAction = new DefaultEventTraceAction(GET_GRANDSON, player);
            traceAction.build(GRANDSON_NUMBER, childData.getId())
                    .build(GRANDSON_QUALITY, childData.getBaseQuality() + "," + childData.getQuality())
                    .build(GRANDSON_INTELLIGENCE, StringUtils.listToString(childData.getFlairList(), ","))
                    .build(GRANDSON_TALENT, StringUtils.listToString(childData.getTalentList(), ",")).trace();
        }catch (Exception e){
            logger.error(" getGrandson ", e);
        }
    }

    /**
     * 书院扩建
     */
    public static void academyExtend(GamePlayer player, int type, int siteNum){
        try{
            DefaultEventTraceAction traceAction = new DefaultEventTraceAction(ACADEMY_EXTEND, player);
            traceAction.build(PICTURE_ID, type)
                    .build(BEFORE_SEAT, siteNum)
                    .build(AFTER_SEAT, siteNum + 1).trace();
        }catch (Exception e){
            logger.error(" academyExtend ", e);
        }
    }

    /**
     * 孙子入学
     */
    public static void academySchoolEntry(GamePlayer player, GrandChildData childData, int schoolType){
        try {
            DefaultEventTraceAction traceAction = new DefaultEventTraceAction(ACADEMY_SCHOOL, player);
            traceAction.build(GRANDSON_NUMBER, childData.getId())
                    .build(SCHOOL_ID, schoolType)
                    .build(START_TIME, DateHelper.getCurrentDateTimeString())
                    .build(IS_END, false)
                    .build(START_SPEED, childData.getEarnSpeed().toString()).trace();

        }catch (Exception e){
            logger.error(" academySchoolEntry ", e);
        }
    }

    /**
     * 孙子出师
     */
    public static void academySchoolGraduate(GamePlayer player, GrandChildData childData, SchoolSiteData data){
        try {
            DefaultEventTraceAction traceAction = new DefaultEventTraceAction(ACADEMY_SCHOOL, player);
            traceAction.build(GRANDSON_NUMBER, childData.getId())
                    .build(SCHOOL_ID, data.getSchoolType())
                    .build(END_TIME, DateHelper.getCurrentDateTimeString())
                    .build(IS_END, true)
                    .build(END_SPEED, childData.getEarnSpeed().toString()).trace();
        }catch (Exception e){
            logger.error(" academySchoolGraduate ", e);
        }
    }

    /**
     * 科举结束
     */
    public static void examination(GamePlayer player, String no, String childNo, String earn, int titile){
        try {
            DefaultEventTraceAction traceAction = new DefaultEventTraceAction(EXAMINATION, player);
            traceAction.build(EXAMINATION_NUMBER, no)
                    .build(GRANDSON_NUMBER, childNo)
                    .build(GRANDSON_SPEED, earn)
                    .build(EXAMINATION_RESULT, titile).trace();
        }catch (Exception e){
            logger.error(" academySchoolGraduate ", e);
        }
    }

    /**
     * 族谱成就
     */
    public static void genealogyAchievement(GamePlayer player, TaskInfo taskInfo, BaseTaskData baseTaskData){
        try {
            DefaultEventTraceAction traceAction = new DefaultEventTraceAction(GENEALOGY_ACHIEVEMENT, player);
            traceAction.build(GRANDSON_ACHIEVEMENT_TYPE, taskInfo.getLittleType())
                    .build(ACHIEVEMENT_ID, baseTaskData.getBaseCondition().getTaskConditionInfo().getTaskId())
                    .build(GRANDSON_QUANTITY, baseTaskData.getBaseCondition().getValue().longValue())
                    .build(RECEIVE_ID, baseTaskData.getBaseCondition().getTaskConditionInfo().getTaskId()).trace();
        }catch (Exception e){
            logger.error(" genealogyAchievement ", e);
        }
    }

    public static final String MOUNTAIN_VILLA = "mountain_villa";

    public static final String MOUNTAIN_VILLAWORK = "mountain_villawork";
    public static final String MOUNTAIN_VILLATEST = "mountain_villatest";

    public static final String MOUNTAIN_VILLAWORKTYPE = "mountain_villaworktype";
    public static final String MOUNTAIN_VILLAWORKLV = "mountain_villaworklv";
    public static final String MOUNTAIN_VILLAWORKADD = "mountain_villaworkadd";
    public static final String MOUNTAIN_VILLATESTTYPE = "mountain_villatesttype";
    public static final String MOUNTAIN_VILLATESTLV = "mountain_villatestlv";


    /**
     * 快照
     */
    public static void uploadSnapshot(GamePlayer player){

        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) return;

            DefaultEventTraceAction action = new DefaultEventTraceAction(MOUNTAIN_VILLA, player);
            //百业
            List<Map<String, Object>> objectGroup = new ArrayList<>();
            GrandChildModule grandChildModule = player.getModule(GrandChildModule.class);
            for (Map.Entry<Integer, GrandChildProfession> entry : grandChildModule.getGrandChildProfessionMap().entrySet()) {
                GrandChildProfession profession = entry.getValue();
                int childOccupation = GrandChildConfigMgr.getChildOccupation(profession.getProfessionId());
                if(childOccupation == 0){
                    continue;
                }
                int professionSkillId = ConfigMgr.getGrandChildProfessionSkill(childOccupation);
                //技能信息
                SkillInfo skillInfo = SkillMgr.getSkillInfo(professionSkillId);
                if(skillInfo == null){
                    continue;
                }
                int add = SkillMgr.getSkillAddition(skillInfo, profession.getSkillLv());
                Map<String, Object> map = new HashMap<>();
                map.put(MOUNTAIN_VILLAWORKTYPE, profession.getProfessionId());
                map.put(MOUNTAIN_VILLAWORKLV, profession.getSkillLv());
                map.put(EventDataConstants.Patrons.GUEST_VOCATION, childOccupation);
                map.put(MOUNTAIN_VILLAWORKADD, add);
                objectGroup.add(map);
            }
            action.build(MOUNTAIN_VILLAWORK, objectGroup);

            //科举
            objectGroup = new ArrayList<>();
            for (Map.Entry<Integer, GrandChildOfficial> officialEntry : grandChildModule.getGrandChildOfficialMap().entrySet()) {
                GrandChildOfficial official = officialEntry.getValue();
                Map<String, Object> map = new HashMap<>();
                map.put(MOUNTAIN_VILLATESTTYPE, official.getOfficialId());
                map.put(MOUNTAIN_VILLATESTLV, official.getGrandChildCount());
                objectGroup.add(map);
            }
            action.build(MOUNTAIN_VILLATEST, objectGroup);

            action.trace();
        }catch (Exception e){
            DataAnalyticsMgr.getLogger().error(" error ", e);
        }

    }

}
